package leetcode;

import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * @date 2020/5/29 22:04
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Autowired {
    int[] count() default 1;
    boolean need() default true;
}

interface M{
//    默认为 public static final
            int a = 1;
    default void test(){
        System.out.println("test m");
    }
    public
    void test2();
}
class N implements M{

    @Override
    public void test2() {
      System.out.println(a);
      this.test();
    }
}
class A {
    public int a1 = 1;
    private int a2 = 2;
    protected int a3 = 3;
}

class B extends A {
    static class D{

    }
    public void test(D d){
        System.out.println(d);
    }
    public int b1 = 4;
    private int b2 = 5;

    @Autowired(count = {5,3},need = false)
    private A a;

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }

    public B() {
        System.out.println("wucan");
    }

    public B(Integer c) {
        System.out.println("youcan");
        this.b2 = c;
    }
}

public class Test {
    public static int test(int a){
        try {
            int i = 1/0;
            return a;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            a = 1;
            a ++;
            return a;
        }
//        return -1;
    }

    public static void main(String[] args) throws Exception {
        M n = new N();
        n.test();
        System.out.println(n.a);
        Class<B> clazz = B.class;
        B b = clazz.newInstance();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            Autowired autowired = field.getDeclaredAnnotation(Autowired.class);
            if(autowired != null){
                System.out.println(Arrays.toString(autowired.count()));
                System.out.println(autowired.need());
                //filed的clazz A
                Class<?> type = field.getType();
                //创建出A实例对象
                Object o = type.newInstance();
                field.set(b,o);

            }
        }
        System.out.println(b.getA());
        System.out.println(test(10));
        System.err.println("==============");
        StringBuffer sb = new StringBuffer("");
        sb.append(1);
        sb.append(12);
        sb.append("a");
        sb.append('c');
        sb.append(new Object());
        sb.append(true);
    }
}
